package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2023/2/14 12:11
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        List<TeachplanDto> teachplanDtos = teachplanMapper.selectTreeNodes(courseId);
        return teachplanDtos;
    }

    public int getTeachplanCount(Long courseId, Long parentId) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }

    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        //通过课程计划id判断是新增和修改

        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if (id != null) {
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        } else {
            //取出同父同级别的课程计划数量
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            //设置排序号
            teachplanNew.setOrderby(count + 1);
            BeanUtils.copyProperties(teachplanDto, teachplanNew);

            teachplanMapper.insert(teachplanNew);

        }

    }

    @Override
    public void deleteTeachplan(Long teachplanId){
      //根据 teachplanId 找到 teachplan  ，再找到media 进行删除
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(Teachplan::getParentid, teachplanId);
        Integer count = teachplanMapper.selectCount(queryWrapper);

        if(count>0){
            //提示应该删除子目录
            System.out.println("count--------------");
            System.out.println(count);
            return ;
        }
        teachplanMapper.deleteById(teachplanId);
        return ;
    }


    @Override
    public void movedown(Long teachplanId){
        //下移
        Teachplan p = teachplanMapper.selectById(teachplanId);
        Integer od= teachplanMapper.selectById(teachplanId).getOrderby();
        System.out.println("od------------------");
        System.out.println(od);
        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<Teachplan>();
        queryWrapper=queryWrapper.eq(Teachplan::getParentid,teachplanMapper.selectById(teachplanId).getParentid()).eq(Teachplan::getCourseId,
                teachplanMapper.selectById(teachplanId).getCourseId());//找到同父集合

        queryWrapper.orderByAsc(Teachplan::getOrderby);
        List<Teachplan> list=teachplanMapper.selectList(queryWrapper);
        System.out.println("list.size()------------------");
        System.out.println(list.size());
       for(int i=0;i<list.size();i++){

            if(list.get(i).getOrderby()>od){
                //交换
                System.out.println("list.get(i).getOrderby()==============");
                System.out.println(list.get(i).getOrderby());

                p.setOrderby(list.get(i).getOrderby());//源对象
                teachplanMapper.updateById(p);
                list.get(i).setOrderby(od);
                teachplanMapper.updateById(list.get(i));
                return;
            }
        }
        XueChengPlusException.cast("不能再往下移动了!");
        return;
    }

    @Override
    public void moveup(Long teachplanId){
        //上移
        Teachplan p = teachplanMapper.selectById(teachplanId);//5  4
        Integer od= teachplanMapper.selectById(teachplanId).getOrderby();// 5
        System.out.println("od------------------");
        System.out.println(od);
        LambdaQueryWrapper<Teachplan> queryWrapper=new LambdaQueryWrapper<Teachplan>();
        queryWrapper=queryWrapper.eq(Teachplan::getParentid,teachplanMapper.selectById(teachplanId).getParentid()).eq(Teachplan::getCourseId,
                teachplanMapper.selectById(teachplanId).getCourseId());//找到同父集合

        queryWrapper.orderByDesc(Teachplan::getOrderby);
        List<Teachplan> list=teachplanMapper.selectList(queryWrapper);
        System.out.println("list.size()------------------");
        System.out.println(list.size());
        for(int i=0;i<list.size();i++){

            if(list.get(i).getOrderby()<od){
                //交换
                System.out.println("list.get(i).getOrderby()==============");
                System.out.println(list.get(i).getOrderby());

                p.setOrderby(list.get(i).getOrderby());//源对象
                teachplanMapper.updateById(p);
                list.get(i).setOrderby(od);
                teachplanMapper.updateById(list.get(i));
                return;
            }
        }
        XueChengPlusException.cast("不能再往上移动了!");
        return;

    }

    @Transactional
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //课程计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan == null){
            XueChengPlusException.cast("课程计划不存在");
            System.out.println("课程计划不存在");
        }

        //先删除原有记录,根据课程计划id删除它所绑定的媒资
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, bindTeachplanMediaDto.getTeachplanId()));

        //再添加新记录
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMediaMapper.insert(teachplanMedia);
        System.out.println("已经保存teachplanMedia");
    }

}
